package com.stefanini.pe.bbva.mbl.in.servlets;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LogServlet extends HttpServlet {

	 
	public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    	try {
    		
    		String txtRutaLog = request.getParameter("txtLog");
    		String rutaLogDescarga = request.getParameter("rutaLogDescarga");
    		
			File fichero = new File(txtRutaLog);
			if(fichero.exists()){
				FileInputStream fis = new FileInputStream(fichero);
				byte[] bytes = new byte[1000];
				int read = 0;
				String fileName = null;
				String contentType = null;
				ServletOutputStream out = null;

				
				fileName = fichero.getName();
				contentType = "text/plain";
				response.setContentType(contentType);
				//response.setContentType("application/x-download");

				response.setHeader("Content-Disposition","attachment;filename=\"" + fileName + "\"");
				out = response.getOutputStream();
			
				while ((read = fis.read(bytes)) != -1) {
					out.write(bytes, 0, read);
				}
			
				out.flush();
				out.close();
				  // System.out.println("\nDescargado\n");
			}else{
				
				response.setHeader("Content-type","text/html");
				
				//ServletOutputStream out  = response.getOutputStream();
				PrintWriter out = response.getWriter(); 
				
				
				out.write("<html>");
				out.write("<head>");
				out.write("<LINK rel='stylesheet' type='text/css' href='/SAAConfig/theme/stylesheet.css' title='Style'>");
				out.write("</head>");
				
				out.write("<body>");
				out.write("<table align='center' width='500' style='background-color:#ffffff'><tr><td align='center' height='20'>");
				out.write("<center><span class='outputError'>");
				
				out.write("No pudo encontrar el archivo: "+txtRutaLog);
				out.write("</span></center>");
				out.write("</td></tr></table>");
				
				out.write("</body>");
				out.write("</html>");
				out.flush();
				out.close();
				
				//ServletOutputStream out = response.getOutputStream();
				//response.setContentType("text/html;charset=UTF-8");
				//PrintWriter out = response.getWriter();
				
				//PrintWriter out = response.getWriter(); 
				//out.println ( "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n" + "<html> \n" + "<head> \n" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \n" + "<title> My first jsp </title> \n" + "</head> \n" + "<body> \n" + "<font size=\"12px\" color=\"" + 
						
				//		"aa"+ "\">" + "Hello World" + "</font> \n" + "</body> \n" + "</html>" ); 
				
				/*out.println("<html>");
				out.println("<head>");
				out.println("<LINK rel='stylesheet' type='text/css' href='/SAAConfig/theme/stylebbva.css' title='Style'>");
				out.println("<LINK rel='stylesheet' type='text/css' href='/SAAConfig/theme/stylesheet.css' title='Style'>");
				out.println("</head>");
				
				out.println("<body>");
				out.println("<table width='300' style='background-color:#ffffff'><tr><td align='center' height='20'>");
				out.println("<center><span class='outputError'>");
				
				out.println("No pudo encontrar el archivo: "+txtRutaLog);
				out.println("</span></center>");
				out.println("</td></tr></table>");
				
				out.println("</body>");
				out.println("</html>");*/
				//out.flush();
				//out.close();
				//this.getServletContext().setAttribute("respuesta", "No pudo encontrar el archivo: "+txtRutaLog);
				//this.getServletContext().getRequestDispatcher(rutaLogDescarga+"/pages/cargarDatos/errorLogDescarga.jsp").forward(request, response);
				
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    
	
	
	
	
}
